<import resource="classpath:alfresco/extension/templates/webscripts/vgr/common.js">

modelPrefix = "{vgr.model}";
//Getting values for the comboboxes in the form
model.valuesForAdministration = listconstraints.getListConstraintValues(modelPrefix + "createdByAdministration");
model.valuesForMajorArea = listconstraints.getListConstraintValues(modelPrefix + "businessMajorArea");
model.valuesForDocType = listconstraints.getListConstraintValues(modelPrefix + "docType");
model.valuesForUnit = listconstraints.getListConstraintValues(modelPrefix + "createdByUnit");
model.valuesForMajorArea = listconstraints.getListConstraintValues(modelPrefix + "businessMajorArea");
model.valuesForMinorArea = listconstraints.getListConstraintValues(modelPrefix + "businessMinorArea");
model.valuesForSortColumn = listconstraints.getListConstraintValues(modelPrefix + "showSortBy");
model.valuesForListType = listconstraints.getListConstraintValues(modelPrefix + "listType");

function getValue(name, searchConditions) {
	for each(condition in  searchConditions) {
		keyAndValue = condition.split(":");
		key = keyAndValue[0];
		value = keyAndValue[1];
		
		if (key == name) {
			return value;
		}
	}
}

function includeColumn(name, columns) {
	for each(column in columns) {
		if (name == column) {
			return true;
		}
	}
	
	return false;
}

savedSearch = null;
model.searchDocType = "";
model.searchDocTitle = "";
model.searchDocId = "";
model.searchDocCreatedBy = "";
model.searchDocDescription = "";
model.searchCreatedbyAuthor = "";
model.searchCreatedbyAdministration = "";
model.searchCreatedByUnit = "";
model.searchResponsibleName = "";
model.searchResponsibleAdministration = "";
model.searchResponsibleUnit = "";
model.searchBusinessMajorArea = "";
model.searchBusinessMinorArea = "";
model.searchBusinessProject = "";
model.searchBusinessAssigner = "";
model.searchReferenceDocumentSuite = "";
model.searchReferenceNumber = "";
model.showAllVersions = "";
model.columnDocId = "";
model.columnDocType = "";
model.columnAuthor = "";
model.columnVersion = "";
model.columnResponsibleName = "";
model.columnPublishedAt = "";
model.showIcon = "";
model.showDescription = "";
model.sortColumn = "";
model.showDescSort = "";

if (args.name != undefined) {
	name =  "+@\\{vgr.model\\}listName:\"" + args.name + "\"";
	path = "((PATH:\"/app:company_home/cm:vgr/cm:Saved_x0020_Searches/*\") AND ";
	type = "(TYPE:\"{vgr.model}savedSearch\"))";
	
	savedSearchResult = search.luceneSearch(name + path + type);
	
	if (savedSearchResult[0] != undefined) {
		savedSearch = savedSearchResult[0];
		searchConditions = savedSearch.properties["vgr:searchCriterias"];
		
		model.searchDocType = getValue("search-doctype", searchConditions);
		model.searchDocTitle = getValue("search-doctitle", searchConditions);
		model.searchDocId = getValue("search-docid", searchConditions);
		model.searchDocCreatedBy = getValue("search-doccreatedby", searchConditions);
		model.searchDocDescription = getValue("search-docdescription", searchConditions);
		
		model.searchCreatedbyAuthor = getValue("search-createdbyauthor", searchConditions);
		model.searchCreatedbyAdministration = getValue("search-createdbyadministration", searchConditions);
		model.searchCreatedByUnit = getValue("search-createdbyunit", searchConditions);
		
		model.searchResponsibleName = getValue("search-responsiblename", searchConditions);
		model.searchResponsibleAdministration = getValue("search-responsibleadministration", searchConditions);
		model.searchResponsibleUnit = getValue("search-responsibleunit", searchConditions);
		
		model.searchBusinessMajorArea = getValue("search-businessmajorarea", searchConditions);
		model.searchBusinessMinorArea = getValue("search-businessminorarea", searchConditions);
		model.searchBusinessProject = getValue("search-businessproject", searchConditions);
		model.searchBusinessAssigner = getValue("search-businessassigner", searchConditions);
		
		model.searchReferenceDocumentSuite = getValue("search-referencedocumentsuite", searchConditions);
		model.searchReferenceNumber = getValue("search-referencenumber", searchConditions);
		
		model.showAllVersions = (savedSearch.properties["vgr:showAllVersions"] == true) ? "checked":"";
		
		columns = savedSearch.properties["vgr:showColumns"];
		model.columnDocId = (includeColumn("vgr:docId", columns) == true) ? "checked":"";
		model.columnDocType = (includeColumn("vgr:docType", columns) == true) ? "checked":"";
		model.columnVersion = (includeColumn("cm:versionLabel", columns) == true) ? "checked":"";
		model.columnAuthor = (includeColumn("vgr:createdByAuthor", columns) == true) ? "checked":"";
		model.columnResponsibleName = (includeColumn("vgr:responsibleName", columns) == true) ? "checked":"";
		model.columnPublishedAt = (includeColumn("vgr:publishedAt", columns) == true) ? "checked":"";
		
		model.showIcon = (savedSearch.properties["vgr:showIcon"] == true) ? "checked":"";
		model.showDescription = (savedSearch.properties["vgr:showDescription"] == true) ? "checked":"";
		
		model.sortColumn = savedSearch.properties["vgr:showSortBy"];
		model.showDescSort = (savedSearch.properties["vgr:showDescSort"] == true) ? "checked":"";
		model.accessZone = savedSearch.properties["vgr:searchAccessZone"];
		
	} else {
		status.code = 404;
		status.message = "Den sparade s&ouml;kningen " + args.name + " hittades inte.";
		status.redirect = true;
	}
}

model.savedSearch = savedSearch;
if (model.savedSearch != null) {
	model.editMode = true;
} else {
	model.editMode = false;
}
	

